package com.bingarchive;

import java.io.StringReader;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

abstract class AbstractParser {
	public interface OnParserListener {
		void onParserFinish(LoadParams loadParams);
	}

	static void ParserXML(String stXML, DefaultHandler handler) throws Exception {
		SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();

		XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();
		xmlReader.setContentHandler(handler);

		xmlReader.parse(new InputSource(new StringReader(stXML)));
	}

	static void Parser(String stXML, LoadParams loadParams, XMLHandler handler, OnParserListener onParserListener) {
		try {
			ParserXML(stXML, handler);
		} catch (Exception e) {
			e.printStackTrace();
			onParserListener.onParserFinish(loadParams);
		}
	}

	static abstract class XMLHandler extends DefaultHandler {
		public LoadParams mLoadParams = null;
		private OnParserListener mOnParserListener = null;

		public XMLHandler(LoadParams loadParams,
				OnParserListener onParserListener) {
			mLoadParams = loadParams;
			mOnParserListener = onParserListener;
		}

		@Override
		public void startDocument() {
		}

		@Override
		public void endDocument() {
			mOnParserListener.onParserFinish(mLoadParams);
		}

	}

}
